package com.tudouji.project.farmer.controller;

import java.util.Date;
import java.util.List;

import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.project.organization.domain.Department;
import com.tudouji.project.organization.service.IDepartmentService;
import com.tudouji.project.system.domain.Tuser;
import com.tudouji.project.system.service.ITuserService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.farmer.domain.Farmerinfo;
import com.tudouji.project.farmer.service.IFarmerinfoService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 种植户档案Controller
 * 
 * @author tudouji
 * @date 2021-04-23
 */
@RestController
@RequestMapping("/farmer/farmerinfo")
public class FarmerinfoController extends BaseController
{
    @Autowired
    private IFarmerinfoService farmerinfoService;
    @Autowired
    private ITuserService tuserService;
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/getAll")
    public AjaxResult getAll(Farmerinfo farmerinfo)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();

        List<Farmerinfo> list = null;
        try {
            list = farmerinfoService.getAll(farmerinfo, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询种植户档案列表
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Farmerinfo farmerinfo)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();

        List<Farmerinfo> list = null;
        try {
            list = farmerinfoService.selectFarmerinfoList(farmerinfo, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getDataTable(list);
    }

    /**
     * 导出种植户档案列表
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerinfo:export')")
    @Log(title = "种植户档案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Farmerinfo farmerinfo)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Farmerinfo> list = null;
        try {
            list = farmerinfoService.export(farmerinfo, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExcelUtil<Farmerinfo> util = new ExcelUtil<Farmerinfo>(Farmerinfo.class);
        return util.exportExcel(list, "farmerinfo");
    }

    /**
     * 获取种植户档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerinfo:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(farmerinfoService.selectFarmerinfoById(guid));
    }

    /**
     * 新增种植户档案
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerinfo:add')")
    @Log(title = "种植户档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Farmerinfo farmerinfo)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        farmerinfo.setOperatorid(tuserId);
        farmerinfo.setGuid(IdUtils.fastSimpleUUID());
        farmerinfo.setStatus(1);
        farmerinfo.setCreateTime(new Date());
        Tuser tuser = tuserService.selectTuserById(farmerinfo.getUserid());
        Department department = departmentService.selectDepartmentById(farmerinfo.getDeptguid());
        farmerinfo.setName(tuser.getRealname());
        farmerinfo.setDepname(department.getName());
        return toAjax(farmerinfoService.insertFarmerinfo(farmerinfo));
    }

    /**
     * 修改种植户档案
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerinfo:edit')")
    @Log(title = "种植户档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Farmerinfo farmerinfo)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        farmerinfo.setOperatorid(tuserId);
        farmerinfo.setLastupdtime(new Date());
        Tuser tuser = tuserService.selectTuserById(farmerinfo.getUserid());
        Department department = departmentService.selectDepartmentById(farmerinfo.getDeptguid());
        farmerinfo.setName(tuser.getRealname());
        farmerinfo.setDepname(department.getName());
        return toAjax(farmerinfoService.updateFarmerinfo(farmerinfo));
    }

    /**
     * 删除种植户档案
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerinfo:remove')")
    @Log(title = "种植户档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(farmerinfoService.deleteFarmerinfoByIds(guids));
    }

    /**
     * @Author shaodong
     * @Description 通过权限获取种植户
     * @Date 14:41 2021/4/23
     * @Param
     * @return
     **/
    @RequestMapping(value = "/getHasPermissionFarmers", method = RequestMethod.GET)
    public AjaxResult getHasPermissionFarmers(){
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        try {
            return AjaxResult.success(farmerinfoService.getHasPermissionFarmers(tuserId));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
